home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #3 / Amiga Plus CD - 2002 - No. 03.iso / AmiSoft / Util / Sys / Amberram.lha / AmberRAM / Source / support.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-01-28  |  7.0 KB  |  464 lines

  1. /*
  2.  
  3. File: support.c
  4. Author: Neil Cafferkey
  5. Copyright (C) 2001-2003 Neil Cafferkey
  6.  
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  20. MA 02111-1307, USA.
  21.  
  22. */
  23.  
  24.  
  25. #include "handler_protos.h"
  26.  
  27.  
  28.  
  29. /****i* ram.handler/SetString **********************************************
  30. *
  31. *   NAME
  32. *    SetString --
  33. *
  34. *   SYNOPSIS
  35. *    block_diff = SetString(field,new_str)
  36. *
  37. *    PINT SetString(TEXT **,TEXT *);
  38. *
  39. *   FUNCTION
  40. *
  41. *   INPUTS
  42. *
  43. *   RESULT
  44. *
  45. *   EXAMPLE
  46. *
  47. *   NOTES
  48. *
  49. *   BUGS
  50. *
  51. *   SEE ALSO
  52. *
  53. ****************************************************************************
  54. *
  55. */
  56.  
  57. PINT SetString(TEXT **field,const TEXT *new_str)
  58. {
  59.    LONG error=0;
  60.    UPINT length=0;
  61.    STRPTR str_copy=NULL,old_str;
  62.    PINT block_diff=0;
  63.  
  64.    /* Allocate new string */
  65.  
  66.    if(new_str!=NULL)
  67.    {
  68.       length=StrSize(new_str);
  69.       if(length>1)
  70.       {
  71.          str_copy=AllocMem(length,MEMF_ANY);
  72.          if(str_copy!=NULL)
  73.          {
  74.             CopyMem(new_str,str_copy,length);
  75.          }
  76.          else
  77.             error=ERROR_DISK_FULL;
  78.       }
  79.       else
  80.          length=0;
  81.    }
  82.  
  83.    if(error==0)
  84.    {
  85.       /* Deallocate old string */
  86.  
  87.       if(length>0)
  88.          block_diff=MEMBLOCKS(length);
  89.       length=0;
  90.       old_str=*field;
  91.       if(old_str!=NULL)
  92.       {
  93.          length=StrLen(old_str)+1;
  94.          FreeMem(old_str,length);
  95.          block_diff-=MEMBLOCKS(length);
  96.       }
  97.  
  98.       /* Store new string */
  99.  
  100.       *field=str_copy;
  101.    }
  102.  
  103.    /* Store error and return difference in block utilisation */
  104.  
  105.    SetIoErr(error);
  106.    return block_diff;
  107. }
  108.  
  109.  
  110.  
  111. /****i* ram.handler/SwapStrings ********************************************
  112. *
  113. *   NAME
  114. *    SwapStrings --
  115. *
  116. *   SYNOPSIS
  117. *    block_diff = SwapStrings(field1,field2)
  118. *
  119. *    PINT SwapStrings(STRPTR *,STRPTR *);
  120. *
  121. *   FUNCTION
  122. *
  123. *   INPUTS
  124. *
  125. *   RESULT
  126. *
  127. *   EXAMPLE
  128. *
  129. *   NOTES
  130. *
  131. *   BUGS
  132. *
  133. *   SEE ALSO
  134. *
  135. ****************************************************************************
  136. *
  137. */
  138.  
  139. PINT SwapStrings(TEXT **field1,TEXT **field2)
  140. {
  141.    STRPTR str1,str2;
  142.    PINT block_diff;
  143.  
  144.    block_diff=0;
  145.  
  146.    str1=*field1;
  147.    if(str1!=NULL)
  148.       block_diff=MEMBLOCKS(StrSize(str1));
  149.  
  150.    str2=*field2;
  151.    if(str2!=NULL)
  152.       block_diff-=MEMBLOCKS(StrSize(str2));
  153.  
  154.    *field1=str2;
  155.    *field2=str1;
  156.  
  157.    /* Return difference in block utilisation */
  158.  
  159.    return block_diff;
  160. }
  161.  
  162.  
  163.  
  164. /****i* ram.handler/StrLen *************************************************
  165. *
  166. *   NAME
  167. *    StrLen --
  168. *
  169. *   SYNOPSIS
  170. *    length = StrLen(s)
  171. *
  172. *    UPINT StrLen(TEXT *);
  173. *
  174. *   FUNCTION
  175. *
  176. *   INPUTS
  177. *
  178. *   RESULT
  179. *
  180. *   EXAMPLE
  181. *
  182. *   NOTES
  183. *
  184. *   BUGS
  185. *
  186. *   SEE ALSO
  187. *
  188. ****************************************************************************
  189. *
  190. */
  191.  
  192. UPINT StrLen(const TEXT *s)
  193. {
  194.    const TEXT *p;
  195.  
  196.    for(p=s;*p!='\0';p++);
  197.    return p-s;
  198. }
  199.  
  200.  
  201.  
  202. /****i* ram.handler/StrSize ************************************************
  203. *
  204. *   NAME
  205. *    StrLen --
  206. *
  207. *   SYNOPSIS
  208. *    size = StrSize(s)
  209. *
  210. *    UPINT StrSize(TEXT *);
  211. *
  212. *   FUNCTION
  213. *
  214. *   INPUTS
  215. *
  216. *   RESULT
  217. *
  218. *   EXAMPLE
  219. *
  220. *   NOTES
  221. *
  222. *   BUGS
  223. *
  224. *   SEE ALSO
  225. *
  226. ****************************************************************************
  227. *
  228. */
  229.  
  230. UPINT StrSize(const TEXT *s)
  231. {
  232.    const TEXT *p;
  233.  
  234.    for(p=s;*p!='\0';p++);
  235.    return p-s+1;
  236. }
  237.  
  238.  
  239.  
  240. /****i* ram.handler/FindNameNoCase *****************************************
  241. *
  242. *   NAME
  243. *    FindNameNoCase --
  244. *
  245. *   SYNOPSIS
  246. *    node = FindNameNoCase(start,name)
  247. *
  248. *    struct Node *FindNameNoCase(struct List *,TEXT *);
  249. *
  250. *   FUNCTION
  251. *
  252. *   INPUTS
  253. *
  254. *   RESULT
  255. *
  256. *   EXAMPLE
  257. *
  258. *   NOTES
  259. *
  260. *   BUGS
  261. *
  262. *   SEE ALSO
  263. *
  264. ****************************************************************************
  265. *
  266. */
  267.  
  268. struct Node *FindNameNoCase(struct List *start,const TEXT *name)
  269. {
  270.    struct Node *node,*next_node,*matching_node;
  271.    STRPTR node_name;
  272.  
  273.    matching_node=NULL;
  274.    node=start->lh_Head;
  275.  
  276.    while(node!=NULL)
  277.    {
  278.       next_node=node->ln_Succ;
  279.       if(next_node!=NULL)
  280.       {
  281.          node_name=node->ln_Name;
  282.          if(node_name!=NULL)
  283.             if(Stricmp(name,node_name)==0)
  284.             {
  285.                matching_node=node;
  286.                next_node=NULL;
  287.             }
  288.       }
  289.       node=next_node;
  290.    }
  291.  
  292.    return matching_node;
  293. }
  294.  
  295.  
  296.  
  297. /****i* ram.handler/MyMakeDosEntry *****************************************
  298. *
  299. *   NAME
  300. *    MyMakeDosEntry --
  301. *
  302. *   SYNOPSIS
  303. *    dos_entry = MyMakeDosEntry(name,type)
  304. *
  305. *    struct DosList *MyMakeDosEntry(TEXT *,LONG);
  306. *
  307. *   FUNCTION
  308. *
  309. *   INPUTS
  310. *
  311. *   RESULT
  312. *
  313. *   EXAMPLE
  314. *
  315. *   NOTES
  316. *
  317. *   BUGS
  318. *
  319. *   SEE ALSO
  320. *
  321. ****************************************************************************
  322. *
  323. */
  324.  
  325. struct DosList *MyMakeDosEntry(const TEXT *name,LONG type)
  326. {
  327.    struct DosList *entry;
  328.    LONG error;
  329.  
  330.    error=0;
  331.    entry=AllocMem(sizeof(struct DosList),MEMF_CLEAR|MEMF_PUBLIC);
  332.  
  333.    if(entry!=NULL)
  334.    {
  335.       if(!MyRenameDosEntry(entry,name))
  336.          error=IoErr();
  337.       entry->dol_Type=type;
  338.    }
  339.    else
  340.       error=IoErr();
  341.  
  342.    if(error!=0)
  343.    {
  344.       MyFreeDosEntry(entry);
  345.       entry=NULL;
  346.    }
  347.  
  348.    SetIoErr(error);
  349.    return entry;
  350. }
  351.  
  352.  
  353.  
  354. /****i* ram.handler/MyFreeDosEntry *****************************************
  355. *
  356. *   NAME
  357. *    MyFreeDosEntry --
  358. *
  359. *   SYNOPSIS
  360. *    MyFreeDosEntry(entry)
  361. *
  362. *    VOID MyFreeDosEntry(struct DosList *);
  363. *
  364. *   FUNCTION
  365. *
  366. *   INPUTS
  367. *
  368. *   RESULT
  369. *
  370. *   EXAMPLE
  371. *
  372. *   NOTES
  373. *
  374. *   BUGS
  375. *
  376. *   SEE ALSO
  377. *
  378. ****************************************************************************
  379. *
  380. */
  381.  
  382. VOID MyFreeDosEntry(struct DosList *entry)
  383. {
  384.    if(entry!=NULL)
  385.    {
  386.       MyRenameDosEntry(entry,NULL);
  387.       FreeMem(entry,sizeof(struct DosList));
  388.    }
  389.  
  390.    return;
  391. }
  392.  
  393.  
  394.  
  395. /****i* ram.handler/MyRenameDosEntry ***************************************
  396. *
  397. *   NAME
  398. *    MyRenameDosEntry --
  399. *
  400. *   SYNOPSIS
  401. *    success = MyRenameDosEntry(entry,name)
  402. *
  403. *    BOOL MyRenameDosEntry(struct DosList *,TEXT *);
  404. *
  405. *   FUNCTION
  406. *
  407. *   INPUTS
  408. *
  409. *   RESULT
  410. *
  411. *   EXAMPLE
  412. *
  413. *   NOTES
  414. *
  415. *   BUGS
  416. *
  417. *   SEE ALSO
  418. *
  419. ****************************************************************************
  420. *
  421. */
  422.  
  423. BOOL MyRenameDosEntry(struct DosList *entry,const TEXT *name)
  424. {
  425.    LONG error;
  426.    UPINT length;
  427.    STRPTR name_copy,old_name;
  428.  
  429.    /* Allocate new string */
  430.  
  431.    error=0;
  432.    name_copy=NULL;
  433.    if(name!=NULL)
  434.    {
  435.       length=StrLen(name);
  436.       name_copy=AllocMem(length+2,MEMF_PUBLIC);
  437.       if(name_copy!=NULL)
  438.       {
  439.          CopyMem(name,name_copy+1,length+1);
  440.          *name_copy=length;
  441.       }
  442.       else
  443.          error=IoErr();
  444.    }
  445.  
  446.    /* Deallocate old string */
  447.  
  448.    if(error==0)
  449.    {
  450.       old_name=BADDR(entry->dol_Name);
  451.       if(old_name!=NULL)
  452.          FreeMem(old_name,*old_name+2);
  453.       entry->dol_Name=MKBADDR(name_copy);
  454.    }
  455.  
  456.    /* Store error code and return success flag */
  457.  
  458.    SetIoErr(error);
  459.    return error==0;
  460. }
  461.  
  462.  
  463.  
  464.